<script lang="ts" setup>
import Search from '@/view/actuator-management/search/index.vue'
import Table from '@/view/actuator-management/table/index.vue'
import {initTableHeight} from '@/assets/js/tableHeight'
import {nextTick, ref} from 'vue'
import {ResData} from '@/interface/ResData'
import {createEmitter} from '@/assets/js/eventBus'
import {deleteActuator, getActuatorList} from "@/api/result/actuatorInfo.ts";
import {ActuatorInfoVo} from "@/interface/vo/task/ActuatorInfoVo.ts";
import {ElMessageBox} from "element-plus";

const emitter = createEmitter()

const pageRef = ref()
const searchRef = ref()
const tableRef = ref()
nextTick(() => {
  initTableHeight(pageRef, searchRef, tableRef, -52)
})
const onSubmit = (form: any) => {
  getTableData(form)
}
const onReset = (form: any) => {
  getTableData(form)
}

const onTableClick = (type: any, id: String) => {
  switch (type) {
    case 'delete':
      onDelete(id)
      break
    default:
      console.warn('Button type cannot be empty')
      break
  }
}

const onDelete = (id: String) => {
  ElMessageBox.confirm(
      '确认要删除吗？',
      '警告',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
    const res = <ResData>await deleteActuator({actuatorNames: id})
    if (res.ok) {
      emitter.emit('refresh')
    }
  }).catch(() => {
  })
}

const tableLoading = ref(false)

const tableData = ref<Array<ActuatorInfoVo>>([])

const getTableData = async (form: {}) => {
  tableLoading.value = true
  const res = <ResData>await getActuatorList(form)
  tableLoading.value = false
  if (res.ok) {
    tableData.value = res.data
  }
}
</script>

<template>
  <div ref="pageRef" class="page">
    <Search ref="searchRef" @onReset="onReset" @onSubmit="onSubmit"/>
    <Table
        ref="tableRef"
        :loading="tableLoading"
        :tableData="tableData"
        @onTableClick="onTableClick">
    </Table>
  </div>
</template>

<style lang="scss" scoped>

</style>
